package com.taplinker.client.im.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;
import com.taplinker.client.R;
import com.taplinker.client.core.ImageManager;
import com.taplinker.client.im.domain.EnumMessageType;
import com.taplinker.client.im.protocol.TapLinkerConversation;
import com.taplinker.client.im.rpc.Cmd012;
import com.taplinker.client.im.util.SmileyParser;
import com.taplinker.core.util.TimeUtil;

public class QueueListAdapter extends BaseAdapter {
	private List<TapLinkerConversation> conversationList;
	private Context context;
	private SmileyParser parser;
	private TapLinkerConversation conversation = new TapLinkerConversation();

	public QueueListAdapter(Context context,
			List<TapLinkerConversation> conversationList) {
		super();
		this.context = context;
		this.conversationList = conversationList;
		this.parser = new SmileyParser(context);
	}

	@Override
	public int getCount() {
		return conversationList.size();
	}

	@Override
	public Object getItem(int position) {
		return conversationList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.queue_list_item, null);
			
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.content = (TextView) convertView.findViewById(R.id.message);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.head = (SmartImageView) convertView.findViewById(R.id.image);
			holder.count = (TextView) convertView.findViewById(R.id.unread);
			holder.delete = convertView.findViewById(R.id.delete);
			holder.mark = (TextView) convertView.findViewById(R.id.txt_mark);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

//		if (!TextUtils.isEmpty(AppConfigFactory.getInstance().getUserInfo()
//				.getUserIcon())) {
//			ImageManager.getInstance().loadImage(
//					ConstantValue.IMAGE
//							+ AppConfigFactory.getInstance().getUserInfo().getUserIcon(), holder.head, 60, 60, null);
//			Log.v("log_ktian", "头像URL：" + ConstantValue.IMAGE + AppConfigFactory.getInstance().getUserInfo().getUserIcon() );
//		}

		final TapLinkerConversation conversation = conversationList
				.get(position);
		String headIcon = conversation.getCustomerIcon();
		
		if(headIcon == null || "".equals(headIcon.trim())) {
//			Log.v("log_ktian", "没有头像显示默认头像");
		}else{
//			Log.v("log_ktian", "头像URL:" + conversation.getCustomerIcon());
//			ImageManager.getInstance().loadImage(conversation.getCustomerIcon(), holder.head, 60, 60, null );
			 holder.head.setImageUrl(conversation.getCustomerIcon());
		}
		
		setMark(holder, conversation);
		setVisitor(holder, conversation);
		setUnreadMsg(holder, conversation);
		setHead(holder, conversation);
		setLastMsg(holder, conversation, position );
		setTime(holder, conversation);
		holder.delete
				.setOnClickListener(new OnClickDelete(conversation.getId()));
//		System.out.println(((TextView) holder.delete).getText() + "我被点击了");
		return convertView;
	}

	private void setHead(ViewHolder holder, TapLinkerConversation conversation) {

		String url = conversation.getPictures();
		ImageManager.getInstance().loadImage(url, holder.head, 60, 60, null);

	}

	private void setVisitor(final ViewHolder holder,
			final TapLinkerConversation conversation) {
//		String name = conversation.getVisitor();
//		if (!TextUtils.isEmpty(conversation.getCustomerId())) {
//			name = conversation.getCustomerName();
//		}
//		if (name!=null && name.length() > 8) {
//			name = name.substring(name.lastIndexOf('_') + 1);
//		}
		String nickName = conversation.getNickName();
		if(nickName==null || "".endsWith(nickName.trim())) {
			nickName = conversation.getSdkAccount();
			if(nickName!=null && !"".equals(nickName.trim())) {
				nickName = nickName.substring(nickName.lastIndexOf('_') + 1);
			}
		}
		holder.name.setText(nickName);
	}
	
	private void setMark(final ViewHolder holder,
			final TapLinkerConversation conversation) {
		String mark = conversation.getProduct().getProductName();
		if(mark != null) {
			holder.mark.setText(mark);
		}
	}

	private void setUnreadMsg(final ViewHolder holder,
			final TapLinkerConversation conversation) {
//System.out.println("conversation.getUnreadMessages:"+conversation.getUnreadMessages());
		if (conversation.getUnreadMessages() > 0) {
			holder.count.setVisibility(View.VISIBLE);
			holder.count.setText(String.valueOf(conversation
					.getUnreadMessages()));
		} else {
			holder.count.setVisibility(View.GONE);
		}
	}

	private void setLastMsg(final ViewHolder holder,
			final TapLinkerConversation conversation, int pos) {
		if (!TextUtils.isEmpty(conversation.getLastMessage())) {
//			if (EnumMessageType.TEXT.getType().equals(
//					conversation.getMessageType())) {
//				CharSequence cs = parser.replace(conversation.getLastMessage());
//				holder.content.setText(cs != null ? cs.toString() : "");
//			} else 
			if (EnumMessageType.IMAGE.getType().equalsIgnoreCase(
					conversation.getMessageType())) {
				holder.content.setText("[图片]");
			} else if (EnumMessageType.AUDIO.getType().equalsIgnoreCase(
					conversation.getMessageType())) {
				holder.content.setText("[语音]");
			}else{
				parser = new SmileyParser(context);
				holder.content.setText(parser.replaceText(conversation.getLastMessage().toString(), "[表情]"));

			}
		} else {
			holder.content.setText("");
		}
	}

	private void setTime(final ViewHolder holder,
			final TapLinkerConversation conversation) {
		if (conversation.getUpdateDate() != null) {
			holder.time.setText(TimeUtil.timeFormat(conversation
					.getUpdateDate()));
		} else {
			holder.time.setText("");
		}
	}

	public List<TapLinkerConversation> getData() {
		return conversationList;
	}

	public void setData(List<TapLinkerConversation> data) {
		conversationList = data;
	}

	private final class OnClickDelete implements View.OnClickListener {

		private String id ;

		public OnClickDelete(String id) {
			super();
			this.id = id;
		}

		@Override
		public void onClick(View v) {
			new Cmd012(id, "", context).execute();
		}

	}

	class ViewHolder {
		public TextView name;
		public TextView content;
		public TextView time;
		public TextView count;
		public TextView mark;
		public SmartImageView head;
		View delete;
	}

}
